package com.shizhanzhe.gxb.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.WXLoginActivity;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.my.activity.AboutActivity;
import com.shizhanzhe.gxb.my.activity.IncomeFormActivity;
import com.shizhanzhe.gxb.my.activity.MyInfoActivity;
import com.shizhanzhe.gxb.my.activity.MyOrderActivity;
import com.shizhanzhe.gxb.my.activity.MyTeamActivity;
import com.shizhanzhe.gxb.my.activity.SettingActivity;
import com.shizhanzhe.gxb.my.activity.TiXianActivity;
import com.shizhanzhe.gxb.my.bean.MyInfo;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.gxb.widget.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by YJ on 2019/1/8.
 */
public class MyFragment extends Fragment {
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    Unbinder unbinder;
    @BindView(R.id.headimg)
    CircleImageView headimg;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.level)
    TextView level;
    @BindView(R.id.id)
    TextView id;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.nologin)
    LinearLayout nologin;
    @BindView(R.id.yugu_today)
    TextView yuguToday;
    @BindView(R.id.yugu_yestoday)
    TextView yuguYestoday;
    @BindView(R.id.yugu_month)
    TextView yuguMonth;
    @BindView(R.id.yugu_old_month)
    TextView yuguOldMonth;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.iv_level)
    ImageView ivLevel;
    @BindView(R.id.ll_level)
    LinearLayout llLevel;

    private MyListener mListener;
    private String totalincome = "";

    public void setListener(MyListener listener) {
        mListener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, view);
        initImmersionBar();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        Log.i("__________", UserBean.headimg);
        Glide.with(getContext()).load(UserBean.headimg).apply(new RequestOptions().error(R.mipmap.head)).into(headimg);
        if (UserBean.uid != 0) {
            nologin.setVisibility(View.VISIBLE);
            nickname.setText("ID:" + UserBean.uid);
            String upid = UserBean.pid == 0 ? "暂无" : UserBean.pid + "";
//            level.setText("普通会员");
            id.setText("邀请码：" + upid);
            income();
        } else {
            nickname.setText("暂未登录");
            nologin.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.order_my, R.id.order_team, R.id.order_team_my, R.id.income, R.id.iv_set, R.id.ll_title, R.id.ll_tx, R.id.about_us, R.id.ll_sy,
            R.id.invite, R.id.iv})
    public void onViewClicked(View view) {
        if (UserBean.uid != 0) {
            switch (view.getId()) {
                //我的订单
                case R.id.order_my:
                    startActivity(new Intent(getContext(), MyOrderActivity.class));
                    break;
                //团队订单
                case R.id.order_team:
                    startActivity(new Intent(getContext(), MyOrderActivity.class).putExtra("type", 1));
                    break;
                //我的团队
                case R.id.order_team_my:
                    startActivity(new Intent(getContext(), MyTeamActivity.class));
                    break;
                //收入报表
                case R.id.income:
                    startActivity(new Intent(getContext(), IncomeFormActivity.class).putExtra("totalincome", totalincome));
                    break;
                //设置
                case R.id.iv_set:
                    startActivity(new Intent(getContext(), SettingActivity.class));
                    break;
                //个人中心
                case R.id.ll_title:
                    startActivity(new Intent(getContext(), MyInfoActivity.class));
                    break;
                //提现
                case R.id.ll_tx:
                    startActivity(new Intent(getContext(), TiXianActivity.class));
                    break;
                //联系我们
                case R.id.about_us:
                    startActivity(new Intent(getContext(), AboutActivity.class));
                    break;
                //我的收益
                case R.id.ll_sy:
                    startActivity(new Intent(getContext(), IncomeFormActivity.class).putExtra("totalincome", totalincome));
                    break;
                //邀请好友
                case R.id.invite:
                case R.id.iv:
                    mListener.inviteclick(0);
                    break;
                default:
            }
        } else {
            startActivity(new Intent(getContext(), WXLoginActivity.class));
        }
    }

    private void income() {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.myinfo(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        MyInfo myInfo = new Gson().fromJson(json, MyInfo.class);
                        money.setText(myInfo.getInfo().getMoney());
                        yuguToday.setText(myInfo.getIncome().getJryg());
                        yuguYestoday.setText(myInfo.getIncome().getZryg());
                        yuguMonth.setText(myInfo.getIncome().getByyg());
                        yuguOldMonth.setText(myInfo.getIncome().getSyyg());
                        totalincome = myInfo.getInfo().getTotalincome() + "";
                        UserBean.level = myInfo.getInfo().getLevel();
//                        UserBean.headimg = myInfo.getInfo().getAvatar();
//                        UserBean.nickname = myInfo.getInfo().getUname();
//                        Glide.with(getContext()).load(Path.BASE_URL + UserBean.headimg).apply(new RequestOptions().error(R.mipmap.head)).into(headimg);
//                        UserBean.level = 1;
                        if (UserBean.level > 0) {
                            iv.setVisibility(View.GONE);
                            level.setText(Html.fromHtml("<font color=\"#ffc358\">运营总监</font>"));
                            llLevel.setBackgroundColor(getResources().getColor(R.color.black_vip));
                            llLevel.setBackground(getResources().getDrawable(R.drawable.shape_gray_20));
                            ivLevel.setImageResource(R.mipmap.my_icon_vip_level);
                        } else {
                            iv.setVisibility(View.VISIBLE);
                            level.setText("普通会员");
                            ivLevel.setImageResource(R.mipmap.my_icon_vip);
                        }
                    } catch (Exception e) {
                        Toast.makeText(getContext(), "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else

                {
                    Toast.makeText(getContext(), "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public interface MyListener {
        void inviteclick(int homeinto);

    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    protected ImmersionBar mImmersionBar;

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar.statusBarView(R.id.bar_0_view)
                .init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        initData();
        if (!hidden && mImmersionBar != null)
            ImmersionBar.with(this)
                    .statusBarView(R.id.bar_0_view)
                    .init();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
